//
//  YXMainController.swift
//  yanXiangWeiBo
//
//  Created by 颜祥 on 15/12/15.
//  Copyright © 2015年 yanxiang. All rights reserved.
//

import UIKit

class YXMainController: UITabBarController
{
    
    
    

    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        // 添加子控制器
        setupChildViewController()
        
        // 自定义tabBar,继承自UITabBar
        let mainTabBar = YXMainTabBar()
        mainTabBar.backgroundColor = UIColor.whiteColor()
        
        // tabBar是只读的，因此只能使用KVC进行赋值
        self.setValue(mainTabBar, forKeyPath: "tabBar")
        
        // 设置标题的颜色
        self.tabBar.tintColor = UIColor.orangeColor()
        
        // 添加中间按钮的点击事件
        mainTabBar.plusButton.addTarget(self, action: "plusBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
    }
    
    func plusBtnDidClick()
    {
        print("__FUNCTION__")
    }
    
    // 添加子控制器
    private func setupChildViewController()
    {
        // 首页
        let homeVC = YXHomeViewController()
        addOneChildViewController(homeVC, title: "首页", norImageName: "tabbar_home")
        homeVC.navigationController?.tabBarItem.badgeValue = "10"
        
        // 消息
        let messageVC = YXMessageViewController()
        addOneChildViewController(messageVC, title: "消息", norImageName: "tabbar_message_center")
        
        // 发现
        let discoverVC = YXDiscoverViewController()
        addOneChildViewController(discoverVC, title: "发现", norImageName: "tabbar_discover")
        
        // 我
        let profileVC = YXProfileViewController()
        addOneChildViewController(profileVC, title: "我", norImageName: "tabbar_profile")
    }
    
    
    // 设置单个子控制器
    private func addOneChildViewController(vc: UIViewController, title: String,  norImageName: String)
    {
        // 1.创建导航控制器
        let nav = UINavigationController(rootViewController: vc)
        vc.title = title
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: norImageName)
        let selectedImageName = norImageName + "_selected"
        vc.tabBarItem.selectedImage = UIImage(named: selectedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        // 2.添加到当前控制器中
        self.addChildViewController(nav)
        
    }

  

}
